package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.Date;

@Service(interfaceClass = LoginService.class)
@Transactional
public class LoginServiceImpl implements LoginService {

    @Autowired
    private ValidateCodeService validateCodeService;
    @Autowired
    private MemberService memberService;

    @Override//用户手机登录
    public Result telephoneLogin(String telephone, String code) {
        //验证验证码是否正确
        boolean b = validateCodeService.checkCode(telephone + RedisMessageConstant.SENDTYPE_LOGIN, code);
        if(!b){
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
        //会员是否注册,根据手机号码查询数据库，是否存在会员
        Member member=memberService.getMemberByPhone(telephone);
        if(member==null){//没有注册过,完成注册
            member=new Member();
            member.setRegTime(new Date());
            member.setPhoneNumber(telephone);
            memberService.add(member);
        }
        return new Result(true,MessageConstant.LOGIN_SUCCESS,member);
    }
}
